//
//  YQReplyCell.m
//  iyuanqi
//
//  Created by zhaoliang chen on 2018/10/31.
//  Copyright © 2018 czl. All rights reserved.
//

#import "YQReplyCell.h"
#import <WebKit/WebKit.h>

@interface YQReplyCell ()
<WKUIDelegate,WKNavigationDelegate>

@property(nonatomic,strong)UIImageView* imHeader;
@property(nonatomic,strong)UILabel* labelName;
@property(nonatomic,strong)UILabel* labelTime;
@property(nonatomic,strong)UILabel* labelContent;

@property(nonatomic,strong)UIScrollView* scrollView;
@property(nonatomic,strong)UIView* scrollContentView;
@property(nonatomic,strong)WKWebView* webContent;

@end

@implementation YQReplyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIView* line = createView([UIColor colorWithHexString:@"f0f2f5"]);
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(self.contentView);
            make.height.mas_equalTo(5);
        }];
        
        [self.contentView addSubview:self.imHeader];
        [self.imHeader mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.top.mas_equalTo(line.mas_bottom).offset(10);
            make.size.mas_equalTo(40);
        }];
        
        [self.contentView addSubview:self.labelName];
        [self.labelName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.imHeader);
            make.left.mas_equalTo(self.imHeader.mas_right).offset(10);
        }];
        
        [self.contentView addSubview:self.labelTime];
        [self.labelTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.imHeader);
            make.right.mas_equalTo(-10);
        }];
        
//        [self.contentView addSubview:self.labelContent];
//        [self.labelContent mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(10);
//            make.right.mas_equalTo(-10);
//            make.bottom.mas_equalTo(-10);
//            make.top.mas_equalTo(self.imHeader.mas_bottom).offset(10);
//        }];
        
        [self.contentView addSubview:self.scrollView];
        [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.contentView);
            make.top.mas_equalTo(self.imHeader.mas_bottom).offset(10);
            make.height.mas_equalTo(100);
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
        }];

        [self.scrollView addSubview:self.scrollContentView];
        [self.scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.scrollView);
            make.width.mas_equalTo(self.scrollView);
        }];

        [self.scrollContentView addSubview:self.webContent];
        [self.webContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.left.mas_equalTo(self.scrollContentView);
            make.height.mas_equalTo(10);
        }];

        [self.scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.webContent.mas_bottom);
        }];

//        [self.contentView addSubview:self.bottomLine];
//        [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(self.webContent.mas_bottom);
//            make.left.right.mas_equalTo(self.contentView);
//            make.height.mas_equalTo(0.1);
//            make.bottom.mas_equalTo(self.contentView);
//        }];
    }
    return self;
}

- (void)setReplyObject:(YQReplyObject *)replyObject {
    _replyObject = replyObject;
    
    self.labelName.text = replyObject.author;
    self.labelTime.text = compareCurrentTime(getIntervalTimeByInterval(replyObject.dateline));
    [self.imHeader sd_setImageWithURL:[NSURL URLWithString:replyObject.avatar] placeholderImage:[UIImage imageNamed:@"loadimage"]];
    [self.webContent loadHTMLString:[self operateHtml:replyObject.message] baseURL:nil];
    //self.labelContent.text = replyObject.message;
}

// 页面加载完成之后调用 此方法会调用多次
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    __block CGFloat webViewHeight;

    //获取内容实际高度（像素）@"document.getElementById(\"content\").offsetHeight;"
    WeakSelf;
    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error) {
        webViewHeight = [result doubleValue];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (webViewHeight != weakSelf.webContent.height) {
                //高度重置
                //weakSelf.webDetail.height = webViewHeight*2;
                //weakSelf.webDetail.scrollView.height = webViewHeight*2;
                [weakSelf.webContent mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(webViewHeight);
                }];
                [weakSelf.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(webViewHeight);
                }];
                UITableView* tableView = (UITableView*)weakSelf.superview;
                [tableView beginUpdates];
                [weakSelf layoutIfNeeded];
                [tableView endUpdates];
            }
        });
    }];
}

- (NSString*)operateHtml:(NSString*)contentStr {
    NSString *result = [NSString stringWithFormat:@"<%@ %@",@"img",@"style='display: block; max-width: 100%;'"];
    contentStr = [contentStr stringByReplacingOccurrencesOfString:@"<img" withString:result];
    NSString *htmlStr = [NSString stringWithFormat:@"<html><head><meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\'/><meta content=\'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;\' name=\'viewport\' /><meta name=\'apple-mobile-web-app-capable\' content=\'yes\'><meta name=\'apple-mobile-web-app-status-bar-style\' content=\'black\'><link rel=\'stylesheet\' type=\'text/css\' /><style type=\'text/css\'> .color{color:#576b95;}</style></head><body><div id=\'content\'>%@</div>", contentStr];
    return htmlStr;
}

- (UIImageView *)imHeader{
    if(!_imHeader){
        _imHeader = ({
            UIImageView * object = [[UIImageView alloc]init];
            object.layer.cornerRadius = 20;
            object.layer.masksToBounds = YES;
            object;
       });
    }
    return _imHeader;
}

- (UILabel *)labelName{
    if(!_labelName){
        _labelName = ({
            UILabel * object = [[UILabel alloc]init];
            object.font = DefaultFont;
            object;
       });
    }
    return _labelName;
}

- (UILabel *)labelTime{
    if(!_labelTime){
        _labelTime = ({
            UILabel * object = [[UILabel alloc]init];
            object.font = DefaultFont;
            object.textAlignment = NSTextAlignmentRight;
            object;
       });
    }
    return _labelTime;
}

- (WKWebView *)webContent{
    if(!_webContent){
        _webContent = ({
            WKWebView * object = [[WKWebView alloc]init];
            object.UIDelegate = self;
            object.navigationDelegate = self;
            object.scrollView.scrollEnabled = NO;
            object.scrollView.bounces = NO;
            object;
       });
    }
    return _webContent;
}

//- (UIView *)bottomLine{
//    if(!_bottomLine){
//        _bottomLine = ({
//            UIView * object = [[UIView alloc]init];
//            object.backgroundColor = [UIColor clearColor];
//            object;
//       });
//    }
//    return _bottomLine;
//}

- (UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = ({
            UIScrollView * object = [[UIScrollView alloc]init];
            object.scrollEnabled = NO;
            object;
       });
    }
    return _scrollView;
}

- (UIView *)scrollContentView{
    if(!_scrollContentView){
        _scrollContentView = ({
            UIView * object = [[UIView alloc]init];
            object;
       });
    }
    return _scrollContentView;
}

- (UILabel *)labelContent{
    if(!_labelContent){
        _labelContent = ({
            UILabel * object = [[UILabel alloc]init];
            object.numberOfLines = 0;
            object.font = DefaultFont;
            object;
       });
    }
    return _labelContent;
}
@end
